package com.gosdk.p;

import java.util.Calendar;
import java.util.List;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

import com.gosdk.p.vo.Offer;
import com.gosdk.p.vo.OfferConfigManager;

public class AdService extends Service {
	
    public static Context mContext;
    
    
    Handler handler = new Handler();
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		
		mContext = this;
	}
	 
	private List<Offer> offers = null;
	int i = 0;
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		
		if (needShowPushAd ()) {
			
			OfferConfigManager.saveAdImpressionTime(AdService.this);
			
			new Thread(new Runnable() {
				
				@Override
				public void run() {
				    
				    
				    try {
						if (needRequestServer()) {
						    try {
						        offers = HttpUtils.requestUserOffers(AdService.this);
						        OfferConfigManager.saveRequestServerTime(AdService.this);
						    } catch (Exception e) {
						        // TODO Auto-generated catch block
						        e.printStackTrace();
						    }
						    i = 1;
						} else {
						    offers = OfferConfigManager.getOfferList(AdService.this);
						    i = 2;
						}
						
						if (offers != null && offers.size() > 0) {
						    handler.post(new Runnable() {
								
								@Override
								public void run() {
									BushManager.pushAction(AdService.this, offers);
								}
							});
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
			}).start();

		    
			Log.d("sdk", "request ad");
		} 
		
		return super.onStartCommand(intent, flags, startId);
	}
	
	/**
	 * 如果达到请求服务器的时间间隔了，不管其他情况，直接返回true
	 * 如果没有达到请求服务器的时间间隔，但是本地offer信息为空，也返回true
	 * @return
	 */
	private boolean needRequestServer() {
	    boolean flg = false;
        // 本地没有offer信息
	    if(OfferConfigManager.getOfferList(this) == null || OfferConfigManager.getOfferList(this).size() == 0) {
	        flg = true;
	    }
	    
	    if (Calendar.getInstance().getTimeInMillis() - OfferConfigManager.getLastRequestServerTime(this) >= 
	            OfferConfigManager.getRequestServerInterval(this)) {
	        
	        flg = true;
	        
	    }
	    
	    
        return flg;
    }

    private boolean needShowPushAd () {
    	Log.d("sdk", "check whether need show ad");
		// 距离上一次广告展示的时间
		if (Calendar.getInstance().getTimeInMillis() - OfferConfigManager.getLastAdImpression(this) < 
		        OfferConfigManager.getImpressionInterval(this)) {
			Log.d("sdk", "show ad skipped: it's not the time");
			return false;
		}
		
		ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		
		if (cm.getActiveNetworkInfo() == null || !cm.getActiveNetworkInfo().isAvailable()) {
			Log.d("sdk", "show ad skipped: network unavailable");
			return false;
		}
		
		return true;
	}
}
